/*
 * mnm.c
 *
 *  Created on: Apr 25, 2013
 *      Author: Daniel
 */

#ifndef IMU_H_
#define IMU_H_

#include "DSP28x_Project.h"

#define MNM_I2C_Address			0xC4	// slave address of magnetometer

// register map

#define MNM_ADDR_DR_STATUS		0x00	// data ready status register
/*	bit	|	set to 1 if
 *	7	|	previous data was not read before new data overwrote it
 *	6	|	previous Z-axis data was not read before new data overwrote it
 *	5	|	previous Y-axis data was not read before new data overwrote it
 *	4	|	previous X-axis data was not read before new data overwrote it
 *	3	|	new data is ready
 *	2	|	new Z-axis data is ready
 *	1	|	new Y-axis data is ready
 *	0	|	new X-axis data is ready
 */

#define MNM_ADDR_OUT_X_MSB		0x01
#define MNM_ADDR_OUT_X_LSB		0x02
#define MNM_ADDR_OUT_Y_MSB		0x03
#define MNM_ADDR_OUT_Y_LSB		0x04
#define MNM_ADDR_OUT_Z_MSB		0x05
#define MNM_ADDR_OUT_Z_LSB		0x06

#define MNM_NUMBYTES_POLL		1		// 1 address
#define MNM_NUMBYTES_READ_ALL	6		// 6 data

#define MNM_ADDR_SYSMOD			0x08

#define MNM_ADDR_OFF_X_MSB		0x09
#define MNM_INIT_OFF_X_MSB		0x00

#define MNM_ADDR_OFF_X_LSB		0x0A
#define MNM_INIT_OFF_X_LSB		0x00

#define MNM_ADDR_OFF_Y_MSB		0x0B
#define MNM_INIT_OFF_Y_MSB		0x00

#define MNM_ADDR_OFF_Y_LSB		0x0C
#define MNM_INIT_OFF_Y_LSB		0x00

#define MNM_ADDR_OFF_Z_MSB		0x0D
#define MNM_INIT_OFF_Z_MSB		0x00

#define MNM_ADDR_OFF_Z_LSB		0x0E
#define MNM_INIT_OFF_Z_LSB		0x00

#define MNM_NUMBYTES_OFFSETS	7		// 1 address + 6 data

#define MNM_ADDR_DIE_TEMP		0x0F

#define MNM_ADDR_CTRL_REG1		0x10
#define MNM_INIT_CTRL_REG1		0b00000 << 3 | 0 << 2 | 0 << 1 | 1 << 0
/*	0bXXXYY << 5	Data rate selection (X) and over sampling ratio (Y).
 * 	Increasing data rate bits decreases sampling frequency but decreases
 * 	current consumption.
 * 	Increasing over sampling ratio bits decreases data output frequency but
 * 	decreases sample noise.
 * 	See table 30 (p15) of the MAG3110 magnetometer datasheet for details.
 *
 * 	X << 2		Fast read selection
 * 	0:		The full 116-values are read.
 * 	1:		Fast read.  8-bit values read from the MSB registers.
 *
 * 	X << 1		Trigger immediate measurement.
 * 	0:		Normal operation based on AC condition.
 * 	1:		Trigger measurement.
 *
 * 	X << 0		Operating mode selection.
 * 	0:		Standby mode.
 * 	1:		Active mode.
 *
 */

#define MNM_ADDR_CTRL_REG2		0x11
#define MNM_INIT_CTRL_REG2		0 | 0 << 7 | 0 << 5 | 0 << 4
/*	X << 7		Automatic magnetic sensor reset.
 * 	0:		Automatic magnetic sensor resets off.
 * 	1:		Automatic magnetic sensor resets on.  The device automatically
 * 	resets its sensors before each data acquisition.
 *
 * 	X << 5		Data output correction.
 * 	0:		Normal mode.  Data values are corrected by the user offset
 * 	register values.
 * 	1:		Raw mode.  Data values are not corrected by the user offset
 * 	register values.
 *
 * 	X << 4		Magnetic sensor reset.
 * 	0:		Reset cycle not active.
 * 	1:		Reset cycle initiate.
 * 	Resets the sensors.  Useful if the device comes in contact with a
 * 	magnetic field that exceeds the full-scale range.
 */

#define MNM_NUMBYTES_CTRL_REGS	3

void InitMNM(void);
void InitMNM_Offsets(void);
void InitMNM_CTRL_REGS(void);
void MNM_read_all(Uint16 * results);


#endif
